namespace Digitalmes.Application.Sys.Dicts.Queries;

/// <summary>
/// 通过字典代码查询命令。
/// </summary>
public record GetDictByCodeQuery(string Code) : IRequest<IResult<SysDictDto>>;

internal sealed class GetDictByCodeQueryHandler(IRepository<SysDict> repo) : IRequestHandler<GetDictByCodeQuery, IResult<SysDictDto>>
{
    public async Task<IResult<SysDictDto>> Handle(GetDictByCodeQuery request, CancellationToken cancellationToken)
    {
        var item = await repo.AsQueryable().Includes(m => m.Items)
            .Where(m => m.Code == request.Code)
            .FirstOrDefaultAsync(cancellationToken);
        return Result.From(item.Adapt<SysDictDto>());
    }
}
